//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package weixin.util.redis;

import com.jeecg.p3.weixinInterface.entity.WeixinAccount;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import org.apache.log4j.Logger;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisPoolUtil {
    public static final String REDIS_WX_ACCOUNT_LIST_KEY = "RES_KEY_WX_ACCOUNT_MAP";
    public static final String REDIS_WX_SUCAI_LINK_PRE = "RES_KEY_SUCAI_LINK_PRE";
    public static final String REDIS_WX_USER_KEY_PRE = "RES_KEY_WX_USER_PRE";
    private static final Logger logger = Logger.getLogger(JedisPoolUtil.class);
    private static JedisPool pool;

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("redis");
        if (bundle == null) {
            throw new IllegalArgumentException("[redis.properties] is not found!");
        } else {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
            config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
            config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
            config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
            config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
            String password = bundle.getString("redis.password");
            String database = bundle.getString("redis.database");
            pool = new JedisPool(config, bundle.getString("redis.ip")
                    , Integer.valueOf(bundle.getString("redis.port"))
                    , 100000
                    , password
                    , StringUtils.isEmpty(database) ? 0 : Integer.valueOf(database));
        }
    }

    public JedisPoolUtil() {
    }

    public static Jedis getJedis() {
        return (Jedis)pool.getResource();
    }

    public static void returnJedis(Jedis jedis) {
        pool.returnResource(jedis);
    }

    public static void resetAllWxAccount(Map<String, WeixinAccount> newWxAccountMap) {
        Jedis jedis = (Jedis)pool.getResource();
        jedis.set("RES_KEY_WX_ACCOUNT_MAP".getBytes(), SerializeUtil.serialize(newWxAccountMap));
        pool.returnResource(jedis);
    }

    public static void clearReis() {
        Jedis jedis = (Jedis)pool.getResource();
        jedis.del(new byte[][]{"RES_KEY_WX_ACCOUNT_MAP".getBytes()});
        Set s = jedis.keys("RES_KEY_SUCAI_LINK_PRE*");
        Iterator it = s.iterator();

        while(it.hasNext()) {
            String key = (String)it.next();
            logger.info("---------------------redis-------------key-------------------" + key);
            jedis.del(new String[]{key});
        }

        pool.returnResource(jedis);
    }

    public static void putWxUser(Map<String, String> userMap) {
        Jedis jedis = (Jedis)pool.getResource();
        String openid = (String)userMap.get("openid");
        String key = "RES_KEY_WX_USER_PRE" + openid;
        jedis.set(key.getBytes(), SerializeUtil.serialize(userMap));
        pool.returnResource(jedis);
    }

    public static Map<String, String> getWxUser(String openid) {
        Jedis jedis = (Jedis)pool.getResource();
        String key = "RES_KEY_WX_USER_PRE" + openid;
        Map<String, String> returnMap = (Map)SerializeUtil.unserialize(jedis.get(key.getBytes()));
        pool.returnResource(jedis);
        return returnMap;
    }

    public static void putWeixinLinkOutUrl(String outUrl, String id) {
        if (RedisConvertUtils.isNotEmpty(outUrl)) {
            Jedis jedis = (Jedis)pool.getResource();
            String key = "RES_KEY_SUCAI_LINK_PRE" + id;
            jedis.set(key, outUrl);
            pool.returnResource(jedis);
        }

    }

    public static String getWeixinLinkOutUrl(String id) {
        if (RedisConvertUtils.isNotEmpty(id)) {
            Jedis jedis = (Jedis)pool.getResource();
            String key = "RES_KEY_SUCAI_LINK_PRE" + id;
            String outurl = jedis.get(key);
            pool.returnResource(jedis);
            return outurl;
        } else {
            return null;
        }
    }

    public static Map<String, WeixinAccount> getAllWxAccount() {
        Jedis jedis = (Jedis)pool.getResource();
        Map<String, WeixinAccount> wxAccountMap = null;
        wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
        pool.returnResource(jedis);
        return wxAccountMap;
    }

    public static void putWxAccount(WeixinAccount po) {
        Jedis jedis = (Jedis)pool.getResource();
        Map<String, WeixinAccount> wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
        if (wxAccountMap == null) {
            wxAccountMap = new HashMap();
        }

        ((Map)wxAccountMap).put(po.getWeixinAccountid(), po);
        jedis.set("RES_KEY_WX_ACCOUNT_MAP".getBytes(), SerializeUtil.serialize(wxAccountMap));
        pool.returnResource(jedis);
    }

    public static void batchPutWxAccount(List<WeixinAccount> accountList) {
        Jedis jedis = (Jedis)pool.getResource();
        Map<String, WeixinAccount> wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
        if (wxAccountMap == null) {
            wxAccountMap = new HashMap();
        }

        Iterator var4 = accountList.iterator();

        while(var4.hasNext()) {
            WeixinAccount po = (WeixinAccount)var4.next();
            ((Map)wxAccountMap).put(po.getWeixinAccountid(), po);
        }

        jedis.set("RES_KEY_WX_ACCOUNT_MAP".getBytes(), SerializeUtil.serialize(wxAccountMap));
        pool.returnResource(jedis);
    }

    public static WeixinAccount getWxAccount(String jwid) {
        WeixinAccount po = null;

        try {
            Jedis jedis = (Jedis)pool.getResource();
            Map<String, WeixinAccount> wxAccountMap = (Map)SerializeUtil.unserialize(jedis.get("RES_KEY_WX_ACCOUNT_MAP".getBytes()));
            if (wxAccountMap != null) {
                po = (WeixinAccount)wxAccountMap.get(jwid);
            }

            pool.returnResource(jedis);
        } catch (Exception var4) {
            logger.error("--JedisPoolUtil--getWxAccount--error--" + var4.toString());
        }

        return po;
    }

    public static void setList(String key, String... values) {
        Jedis jedis = (Jedis)pool.getResource();
        jedis.sadd(key, values);
        pool.returnResource(jedis);
    }

    public static boolean exitInList(String key, String value) {
        Jedis jedis = (Jedis)pool.getResource();
        boolean b = jedis.sismember(key, value);
        pool.returnResource(jedis);
        return b;
    }

    public static Object getList(String key) {
        Jedis jedis = (Jedis)pool.getResource();
        Set<String> smembers = jedis.smembers(key);
        pool.returnResource(jedis);
        return smembers;
    }

    public static Long clearList(String key) {
        Jedis jedis = (Jedis)pool.getResource();
        Long i = jedis.del(new String[]{key});
        pool.returnResource(jedis);
        return i;
    }

    public static void main(String[] args) {
        clearList("ip_list");
        setList("ip_list", "192.168.0.115", "192.168.0.116", "192.168.0.116");
        System.out.println(getList("ip_list"));
        setList("ip_list", "192.168.0.200");
        System.out.println(getList("ip_list"));
        System.out.println(exitInList("ip_list", "192.168.0.116"));
    }

    public static void maintest(String[] args) {
        Jedis jedis = (Jedis)pool.getResource();
        WeixinAccount po = new WeixinAccount();
        po.setId("sdfsdf-sdfsdfsdf");
        po.setAccountaccesstoken("100");
        po.setAccountappsecret("200");
        String keys = "name";
        jedis.set(po.getId().getBytes(), SerializeUtil.serialize(po));
        WeixinAccount tt = (WeixinAccount)SerializeUtil.unserialize(jedis.get(po.getId().getBytes()));
        System.out.println(tt.getAccountaccesstoken());
        pool.returnResource(jedis);
    }
}
